package com.cart.service.Impl;

import com.cart.dao.CartDao;
import com.cart.exception.MsgException;
import com.cart.service.CartService;
import com.cart.service.ProductService;
import com.common.domain.Cart;
import com.common.domain.Product;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Elias
 * @version 1.0
 * @date 2021-06-02 11:19
 * @description
 * @copyright 2021
 */
@Service
public class CartServiceImpl implements CartService {
  @Autowired private CartDao cartDao;

  @Resource private ProductService productService;

  @Autowired private ObjectMapper objectMapper;

  @Override
  public List<Cart> queryCart(String userId) {
    return cartDao.selectCart(null, userId, null, null, null, null, null);
  }

  @Transactional(rollbackFor = MsgException.class)
  @Override
  public void addCart(Cart cart) {
    String productId = cart.getProductId();
    Product product = productService.queryProduct(productId);
    if (product == null) {
      throw new MsgException("商品不存在");
    }
    Integer num = cart.getNum();
    //    暂存num
    cart.setNum(null);
    List<Cart> carts = cartDao.selectCart(cart);
    if (carts.isEmpty() || carts.size() == 0) {
      // TODO 购物车中还没有这个商品，执行 insert into
      cart.setProductImage(product.getProductImageurl());
      cart.setProductName(product.getProductName());
      cart.setProductPrice(product.getProductPrice());
      cart.setNum(num);
      cartDao.insertSelective(cart);
    }
    cart.setNum(num);
    cartDao.insertSelective(cart);
    if (cart.getNum() > product.getProductNum()) {
      throw new MsgException(String.format("购买数量不能大于库存%d", product.getProductNum()));
    }
  }

  @Transactional(rollbackFor = MsgException.class)
  @Override
  public void updateCart(Cart cart) {
    cartDao.updateByPrimaryKeySelective(cart);
    String productId = cart.getProductId();
    Product product = productService.queryProduct(productId);
    if (product == null) {
      throw new MsgException("商品不存在");
    }
    if (cart.getNum() > product.getProductNum()) {
      throw new MsgException(String.format("购买数量不能大于库存%d", product.getProductNum()));
    }
  }

  @Override
  public void deleteCart(Cart cart) {
    cartDao.deleteByPrimaryKey(cart.getId());
  }

  @Override
  public Double totalPrice(String productId, String productNums, String userId) {
    try {
      String[] productIds = objectMapper.readValue(productId, String[].class);
      Integer[] productNum = objectMapper.readValue(productNums, Integer[].class);
      Double money = 0.0D;
      if (productIds.length != productNum.length) {
        throw new MsgException("传递参数有误");
      }
      for (int i = 0; i < productIds.length; i++) {
        String pId = productIds[i];
        Integer num = productNum[i];
        Product product = productService.queryProduct(pId);
        if (product == null || num == null) {
          throw new MsgException("商品查询有误");
        }
        if (num > product.getProductNum()) {
          throw new MsgException("库存不足，库存" + product.getProductNum() + "件，购买" + num + "件");
        }
        List<Cart> carts = cartDao.selectCart(null, userId, pId, null, null, null, null);
        Cart cart = carts.get(0);
        money = money + (num * cart.getProductPrice());
      }
      return money;
    } catch (MsgException e) {
      e.printStackTrace();
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
    }
    return null;
  }
}
